﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FtpConfig;

namespace FolderSyncApp
{
    public partial class frmSettings : Form
    {
        private FolderSyncConfig _config = new FolderSyncConfig();
        public frmSettings()
        {
            InitializeComponent();
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            txtFolderSync.Text = _config.FolderSyncParam;
            txtFtpServer.Text = _config.FtpServer;
            txtUsername.Text = _config.Username;
            txtPassword.Text = _config.Password;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _config.FolderSyncParam = txtFolderSync.Text;
            _config.FtpServer = txtFtpServer.Text;
            _config.Username = txtUsername.Text;
            _config.Password = txtPassword.Text;
            _config.UpdateConfig();
            MessageBox.Show("Cập nhật thành công!");
            this.Close();
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTestFTP_Click(object sender, EventArgs e)
        {
            try
            {
                FtpConnection ftp = new FtpConnection(txtFtpServer.Text, txtUsername.Text, txtPassword.Text);
                ftp.Open(); /* Open the FTP connection */
                ftp.Login(); /* Login using previously provided credentials */
                ftp.Close();
                MessageBox.Show("Kiểm tra kết nối FTP thành công!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kiểm tra kết nối FTP thất bại!");
            }
            
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            //mo form cho nguoi ta chon co so du lieu
            FolderBrowserDialog browser = new FolderBrowserDialog();
            if (browser.ShowDialog() == DialogResult.OK)
                txtFolderSync.Text = browser.SelectedPath;
        }
    }
}
